/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package zpsb.accounting.session;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import zpsb.accounting.model.User;

/**
 *
 * @author Michal
 */
@Stateless
public class Authenticator implements AuthenticatorLocal {

    @PersistenceContext
    EntityManager em;
    
     @Override
     public Boolean authenticate(final String username, final String password) {
        TypedQuery<User> q = em.createQuery("select u from User u where u.username = :a1 "
                + "and u.password = :password", User.class);
        q.setParameter("a1", username);
        q.setParameter("password", password);
        
        try {
            return q.getSingleResult() != null;
        } catch (NoResultException e) {
            return false;
        }
    }

}
